elasticsearch-dump 迁移es数据 (elasticdump)

elasticsearch 部分查询语句

1
2
3
4
5
6
7
8
9
# 获取集群的节点列表:
curl 'localhost:9200/_cat/nodes?v'

# 列出所有索引:
curl 'localhost:9200/_cat/indices?v'

创建一个名为“customer”的索引,然后再查看所有的索引:
curl -X PUT 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'

elasticsearch-dump

https://github.com/taskrabbit/elasticsearch-dump

Install

npm 环境准备
1
2
3
4
5
6
7
8
9
10
11
Installing
(local)
wget https://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar.xz

tar xf node-v8.11.2-linux-x64.tar.xz

mv node-v8.11.2-linux-x64 /usr/local

ln -s /usr/local/node-v8.11.2-linux-x64/bin/npm /usr/local/bin/npm

ln -s /usr/local/node-v8.11.2-linux-x64/bin/node /usr/local/bin/node
继续安装
1
2
3
4
5
6
7
8
9
10
npm init -f

#全局或者局部

npm install elasticdump
./bin/elasticdump
(global)

npm install elasticdump -g
elasticdump

数据迁移

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'#拷贝analyzer分词
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=analyzer
'#拷贝映射
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=mapping
'#拷贝数据
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=http://staging.es.com:9200/my_index \
--type=data


# 注意 elasticdump 提供给了--httpAuthFile 参数来做认证
--httpAuthFile When using http auth provide credentials in ini file in form
`user=<username>
password=<password>`

# 只需要写一个ini文件 ,文件中写入用户名和密码就可以了
# 这里其实还有另外一个好的方法
# 在--input参数和--output参数的的url中添加账号密码
# 例如

elasticdump \
--input=http://prod-username:prod-passowrd@production.es.com:9200/my_index \
--output=http://stage-username:stage-password@staging.es.com:9200/my_index \
--type=data

导出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 备份索引数据到文件里:
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=/data/my_index_mapping.json \
--type=mapping
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=/data/my_index.json \
--type=data

# 备份到标准输出,且进行压缩(这里有一个需要注意的地方,我查询索引信息有6.4G,用下面的方式备份后得到一个789M的压缩文件,这个压缩文件解压后有19G):
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=$ \
| gzip > /data/my_index.json.gz

# 把一个查询结果备份到文件中
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=query.json \
--searchBody '{"query":{"term":{"username": "admin"}}}'

导入数据

1
2
3
4
# 将备份文件的数据导入ES
elasticdump \
--input=./data.json \
--output=http://es.com:9200
------ 本文结束 ------

版权声明

Medivh's Notes by Medivh is licensed under a Creative Commons BY-NC-ND 4.0 International License.
Medivh创作并维护的Medivh's Notes博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证
本文首发于Medivh 博客( http://www.mknight.cn ),版权所有,侵权必究。